{layout name="layout" /}
<div>
    <h4>公共方法</h4>
<textarea readonly class="width">
//打印数组
function dd($data){
    echo "<pre>";
    if(empty($data)){
        var_dump($data);
        return;
    }
    if(is_object($data)){
        $data = $data->toArray();
        foreach($data as $k=>$v){
            if(is_object($v)){
                $data[$k] = $v->toArray();
            }
        }
        print_r($data);
        return;
    }
    print_r($data);
    return;
}
//请求返回json
function res($code = 0, $data = '',$message = ''){
    //调用extra/code配置文件中定义好的常量
    $res = [
        'code' => config("code.".$code)['code'],
        'message' => config("code.".$code)['message'],
        'data' => $data,
    ];
    if(!empty($message)){
        $res['message'] = $message;
    }
    return json($res);
}
//请求返回string
function returnstr($code = 0, $message = ''){
    //调用extra/code配置文件中定义好的常量
    $res = config("code.".$code)['message'];
    if(!empty($message)){
        $res = $message;
    }
    return $res;
}
//递归
function recursive($list, $parent_id=0){
    $arr = array();
    foreach($list as $l){
        if($l['parent_id'] == $parent_id){
            $arr[] = $l;
            $child = recursive_menu($list, $l['menu_id']);
            if(is_array($child)){
                $arr = array_merge($arr, $child);
            }
        }
    }
    return $arr;
}
</textarea>
</div>
